﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Qiao.Demo.FileServer.Controllers
{
    public class ImageController : Controller
    {
        //http://localhost:39663/image/UploadImage
        // GET: Image
        public ActionResult Index()
        {
            return View();
        }

        #region UploadImage
        public ActionResult UploadImage(string categoryName)
        {
            string filePath = "";
            try
            {
                foreach (string item in Request.Files)
                {
                    HttpPostedFileBase postFile = Request.Files[item];
                    if (postFile.ContentLength == 0)
                    {
                        continue;
                    }

                    if (categoryName == null)
                    {
                        categoryName = string.Format("{0:yyyyMMdd}", DateTime.Today);
                    }
                    ///  "/Images/20180621"
                    ///   Image + 上当前 年 月 日
                    string newFilePath = "/Images/" + categoryName;
                    //判断项目根目录下是否存在对应的文件夹
                    if (!System.IO.Directory.Exists(Server.MapPath(newFilePath)))
                    {
                        System.IO.Directory.CreateDirectory(Server.MapPath(newFilePath));
                    }
                    //设置 文件路径 + 文件名称
                    string file = newFilePath + "/" + string.Format("{0:hhmmss}", DateTime.Now) + "_" + System.Guid.NewGuid() + "." + postFile.FileName.Split('.').Last();
                    filePath = file;

                    if (postFile.ContentLength == 0)
                    {
                        continue;
                    }
                    //保存文件
                    postFile.SaveAs(Server.MapPath(filePath));
                }
            }
            catch (Exception ex)
            {

            }
            return Content(filePath);
        }
        #endregion

        #region UploadVideo
        public ActionResult UploadVideo(string categoryName)
        {
            string filePath = "";
            try
            {
                foreach (string item in Request.Files)
                {
                    #region 无用
                    //HttpPostedFileBase postFile = Request.Files[item];
                    //if (postFile.ContentLength == 0)
                    //    continue;

                    ////文件名  
                    //string fileName = Guid.NewGuid().ToString();
                    ////文件扩展名
                    //string fileExtension = postFile.FileName.Substring(postFile.FileName.LastIndexOf("."), (postFile.FileName.Length - postFile.FileName.LastIndexOf(".")));  //扩展名  .mp4

                    //Stream stream = postFile.InputStream;

                    //string key = fileName + fileExtension;

                    //UploadInfo uploadInfo = new UploadInfo()
                    //{
                    //    ContentLength = postFile.ContentLength,
                    //    FileName = fileName,
                    //    UploadedLength = 0,
                    //    IsReady = true,
                    //    Key = key
                    //};

                    //PutExtra put = new PutExtra();
                    //string strFileLocalPath = Server.MapPath("./Upload//Tmp//");
                    //string Url = helper.ResumeUploadFile(stream, key, put, strFileLocalPath);
                    //filePath = "/" + Url; 
                    #endregion

                    HttpPostedFileBase postFile = Request.Files[item];
                    if (postFile.ContentLength == 0)
                    {
                        continue;
                    }

                    if (categoryName == null)
                    {
                        categoryName = string.Format("{0:yyyyMMdd}", DateTime.Today);
                    }
                    ///  "/Images/20180621"
                    ///   Image + 上当前 年 月 日
                    string newFilePath = "/Video/" + categoryName;
                    //判断项目根目录下是否存在对应的文件夹
                    if (!System.IO.Directory.Exists(Server.MapPath(newFilePath)))
                    {
                        System.IO.Directory.CreateDirectory(Server.MapPath(newFilePath));
                    }
                    //设置 文件路径 + 文件名称
                    string file = newFilePath + "/" + string.Format("{0:hhmmss}", DateTime.Now) + "_" + System.Guid.NewGuid() + "." + postFile.FileName.Split('.').Last();
                    filePath = file;

                    if (postFile.ContentLength == 0)
                    {
                        continue;
                    }
                    //保存文件
                    postFile.SaveAs(Server.MapPath(filePath));
                }
            }
            catch (Exception e)
            {
                return Content("");
            }
            return Content(filePath);
        }
        #endregion

    }
}